package TestPaperSystem.utils.email;


import TestPaperSystem.utils.RedisUtils;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class CodeCreator {
    private final static String[] patch = {"000000","00000","0000","000","00","0",""};
    private static CodeCreator instance;

    public static CodeCreator getInstance() {
        if(instance == null){
            instance = new CodeCreator();
        }
        return instance;
    }

    public String getCode(String email){


        int tele_hashcode = email.hashCode();

        long currentTime = System.currentTimeMillis();
        long result = tele_hashcode ^ currentTime;

        result = result % 1000000;

        result = result > 0 ? result : -result;

        String codeStr = String.valueOf(result);
        RedisUtils.redis.opsForValue().set("code"+email,codeStr,2, TimeUnit.MINUTES);
        System.out.println("code"+email + "生成时:" + codeStr);
        return patch[codeStr.length()] + result;
    }


}
